package com.example.riceexambackend.common.pojo;

import com.example.riceexambackend.common.errorcode.CommonErrorCodeConstants;
import com.example.riceexambackend.common.errorcode.ErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回对象
 * @param <T> 封装数据类型
 */
@Data
public class CommonResult<T> implements Serializable {
    private Integer code; // 错误码
    private T data; // 携带数据
    private String msg; // 信息

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.code = CommonErrorCodeConstants.SUCCESS.getCode();
        commonResult.msg = CommonErrorCodeConstants.SUCCESS.getMsg();
        commonResult.data = data;
        return commonResult;
    }

    public static <T> CommonResult<T> fail(Integer code, String msg) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.code = code;
        commonResult.msg = msg;
        return commonResult;
    }

    public static <T> CommonResult<T> fail(ErrorCode errorCode) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.code = errorCode.getCode();
        commonResult.msg = errorCode.getMsg();
        return commonResult;
    }

}
